/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateChangeAirportView.java
 *
 * Created on 19-jan-2010, 14:13:19
 */
package View;

import Model.Airport;
import Util.StringToHtmlConverter;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Jeroen
 */
public class CreateChangeAirportView extends javax.swing.JInternalFrame {

    private Airport airport = null;
    boolean newAirport;

    /**
     * Creates new form CreateChangeAirportView
     */
    public CreateChangeAirportView(Airport airport) {
        initComponents();
        this.setTitle("Add Airport");

        if (airport != null) {
            this.airport = airport;
            this.newAirport = false;
            this.txtCity.setText(airport.getCity());
            this.txtCountry.setText(airport.getCountry());
            this.txtName.setText(airport.getName());
            this.txtCode.setText(airport.getCode());
            this.txtLandcode.setText(airport.getLandcode());
            this.txtRegion.setText(String.valueOf(airport.getRegion()));
            this.setTitle("Change Airport");
        } else {
            this.airport = new Airport();
            this.newAirport = true;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        lblCountry = new javax.swing.JLabel();
        lblCity = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtCountry = new javax.swing.JTextField();
        txtCity = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblError = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        lblCode = new javax.swing.JLabel();
        lblLandcode = new javax.swing.JLabel();
        lblRegion = new javax.swing.JLabel();
        txtRegion = new javax.swing.JTextField();
        txtLandcode = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setMaximumSize(null);
        setMinimumSize(null);
        setName("Form"); // NOI18N
        setOpaque(true);
        setPreferredSize(new java.awt.Dimension(300, 239));
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e1) {
            e1.printStackTrace();
        }

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(flyaway.FlyAWayApp.class).getContext().getResourceMap(CreateChangeAirportView.class);
        lblName.setText(resourceMap.getString("lblName.text")); // NOI18N
        lblName.setName("lblName"); // NOI18N

        lblCountry.setText(resourceMap.getString("lblCountry.text")); // NOI18N
        lblCountry.setName("lblCountry"); // NOI18N

        lblCity.setText(resourceMap.getString("lblCity.text")); // NOI18N
        lblCity.setName("lblCity"); // NOI18N

        txtName.setText(resourceMap.getString("txtName.text")); // NOI18N
        txtName.setName("txtName"); // NOI18N

        txtCountry.setText(resourceMap.getString("txtCountry.text")); // NOI18N
        txtCountry.setName("txtCountry"); // NOI18N

        txtCity.setText(resourceMap.getString("txtCity.text")); // NOI18N
        txtCity.setName("txtCity"); // NOI18N

        btnSave.setText(resourceMap.getString("btnSave.text")); // NOI18N
        btnSave.setName("btnSave"); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblError.setText(resourceMap.getString("lblError.text")); // NOI18N
        lblError.setName("lblError"); // NOI18N

        txtCode.setText(resourceMap.getString("txtCode.text")); // NOI18N
        txtCode.setName("txtCode"); // NOI18N

        lblCode.setLabelFor(txtCode);
        lblCode.setText(resourceMap.getString("lblCode.text")); // NOI18N
        lblCode.setName("lblCode"); // NOI18N

        lblLandcode.setText(resourceMap.getString("lblLandcode.text")); // NOI18N
        lblLandcode.setName("lblLandcode"); // NOI18N

        lblRegion.setText(resourceMap.getString("lblRegion.text")); // NOI18N
        lblRegion.setName("lblRegion"); // NOI18N

        txtRegion.setName("txtRegion"); // NOI18N

        txtLandcode.setName("txtLandcode"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblLandcode)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(lblName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblCountry, javax.swing.GroupLayout.PREFERRED_SIZE, 46, Short.MAX_VALUE)
                                .addComponent(lblCity, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblCode, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblRegion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(btnSave))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnCancel)
                            .addComponent(txtRegion, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                            .addComponent(txtLandcode)
                            .addComponent(txtCity)
                            .addComponent(txtCountry)
                            .addComponent(txtCode)
                            .addComponent(txtName)))
                    .addComponent(lblError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCode)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblName)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCountry)
                    .addComponent(txtCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCity)
                    .addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblLandcode)
                    .addComponent(txtLandcode, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRegion)
                    .addComponent(txtRegion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblError, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txtCity, txtCountry, txtName});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean saveAirport() {
        String name = txtName.getText();
        String country = txtCountry.getText();
        String city = txtCity.getText();
        String code = txtCode.getText();
        Integer region = null;
        String landcode = txtLandcode.getText();

        ArrayList<String> errorMessages = new ArrayList<>();


        name = name.trim();
        country = country.trim();
        city = city.trim();
        code = code.trim();
        landcode = landcode.trim();

        if (name.isEmpty()) {
            errorMessages.add("Please fill in a name");
        }
        if (txtRegion.getText().isEmpty()) {
            errorMessages.add("Please fill in a region");
        } 
            try {
                region = Integer.parseInt(txtRegion.getText());
            } catch (NumberFormatException e) {
                errorMessages.add("Region can only be numbers");
            }
        
        if (landcode.isEmpty()) {
            errorMessages.add("Please fill in a landcode");
        }
        if (landcode.length() > 5) {
            errorMessages.add("Landcode may only be 5 characters long");
        }

        if (country.isEmpty()) {
            errorMessages.add("Please fill in a country");
        }

        if (city.isEmpty()) {
            errorMessages.add("Please fill in a city");
        }

        if (code.isEmpty()) {
            errorMessages.add("Please fill in a code");
        }
       try{
        if(region < 0){
            errorMessages.add("Regioncode cannot be a negative number");
        }}catch(Exception ex){  
        }
        

        if (!errorMessages.isEmpty()) {
            lblError.setText(StringToHtmlConverter.format(errorMessages));
            return false;
        }


        if (newAirport) {
            airport.setCity(city);
            airport.setCountry(country);
            airport.setName(name);
            airport.setCode(code);
            airport.setRegion(region);
            airport.setLandcode(landcode);

            if (Controller.Controller.Instance().save(airport)) {
                JOptionPane.showMessageDialog(this, "Airport " + airport.getName() + " saved");
                return true;
            } else {
                lblError.setText("Unable to save airport");
            }
        } else {
            airport.setCity(city);
            airport.setCountry(country);
            airport.setName(name);
            airport.setCode(code);
            airport.setRegion(region);
            airport.setLandcode(landcode);

            if (Controller.Controller.Instance().update(airport)) {
                JOptionPane.showMessageDialog(this, "Airport " + airport.getName() + " saved");
                return true;
            } else {
                lblError.setText("Unable to change Airport");
            }
        }

        return false;
    }
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (this.saveAirport()) {
            this.dispose();
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel lblCity;
    private javax.swing.JLabel lblCode;
    private javax.swing.JLabel lblCountry;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblLandcode;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblRegion;
    private javax.swing.JTextField txtCity;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtCountry;
    private javax.swing.JTextField txtLandcode;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtRegion;
    // End of variables declaration//GEN-END:variables
}
